import axios from '@/utils/request'
const ORDER_STATUS = {
  '-2': '支付失败',
  '-1': '已取消',
  1: '待支付',
  2: '已支付',
  3: '已发货',
  4: '已结算',
  5: '已完成'
}
export default {
  OrderItem: {
    form: {
      fields: [
        {
          property: 'specification',
          field_options: { label: '销售规格', disabled: true },
        },
        {
          property: 'quantity',
          field_options: { label: '购买数量' },
          disabled: true
        },
        {
          property: 'cost',
          field_options: { label: '合计成本' }
        },
        {
          property: 'profit',
          field_options: { label: '利润' }
        }
      ]
    },
    list: {
      disabled_actions: ['delete', 'new'],
      query: {
        '@filter': `entity.getOrder().getStatus() >= 2`,
        '@order': 'entity.id|DESC',
        '@expands': "['entity.order.store','entity.order.staff']"
      },
      list_filter: {
        'order.store.id': () => {
          return axios
            .get(`/manage/stores`)
            .then(res =>
              Object.assign({ __label: '门店' },
                ...res.data.map(v => { return { [v.id]: v.name } })))
        },
        'specification.id': () => {
          return axios
            .get(`/manage/specifications`, { params: { limit: 10000 } })
            .then(res =>
              Object.assign({ __label: '产品' },
                ...res.data.map(v => { return { [v.id]: v.name } })))
        },
        'order.staff.name': '销售',
        beforeCreatedTime: {
          expression: 'entity.getCreatedTime() >= datetime.get(":value")',
          label: '下单开始时间',
          type: 'datetime'
        },
        afterCreatedTime: {
          expression: 'entity.getCreatedTime() < datetime.get(":value")',
          label: '下单结束时间',
          type: 'datetime'
        }
      },
      list_display: [
        {
          property: 'createdTime',
          label: '时间',
          component: {
            props: ['record'],
            data() {
              return {
                createdTime: ''
              }
            },
            created() {
              const date = new Date(this.record.order.__metadata.createdTime)
              this.createdTime = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
            },
            render(h) {
              return (
                <div>
                  {this.createdTime}
                </div>
              )
            }
          }
        },
        {
          property: 'store',
          label: '门店',
          component: {
            props: ['record'],
            render(h) {
              return (
                <div>
                  {this.record.order.__metadata.store.__toString}
                </div>
              )
            }
          }
        },
        {
          property: 'staff',
          label: '业务',
          component: {
            props: ['record'],
            render(h) {
              return (
                <div>
                  {this.record.order.__metadata.staff.__toString}
                </div>
              )
            }
          }
        },
        {
          property: 'staff',
          label: '全职/兼职',
          component: {
            props: ['record'],
            render(h) {
              if (this.record.order.__metadata.staff.__metadata.weightType === 1) {
                return (
                  <div>
                    全职
                  </div>
                )
              }
              if (this.record.order.__metadata.staff.__metadata.weightType === 0) {
                return (
                  <div>
                    兼职
                  </div>
                )
              }
            }
          }
        },
        {
          property: 'status',
          label: '订单状态',
          component: {
            props: ['record'],
            render(h) {
              return (
                <div>
                  {ORDER_STATUS[this.record.order.__metadata.status]}
                </div>
              )
            }
          }
        },
        'specification',
        'quantity',
        {
          property: 'unitPrice',
          label: '销售单价'
        },
        {
          property: 'price',
          label: '合计售价'
        },
        {
          property: 'soecification',
          label: '当前成本',
          component: {
            props: ['record'],
            data() {
              return {
                stockCost: 0
              }
            },
            created() {
              // 获取订单门店对应产品的库存成本
              this.axios.get(`/manage/stocks`, {
                params: {
                  '@filter': `entity.getSpecification().getId() == ${this.record.specification.id} && entity.getStore().getId() == ${this.record.order.__metadata.store.id}`
                }
              }).then((res) => {
                console.log('库存信息', res.data)
                if (res.data && res.data.length > 0) {
                  this.stockCost = res.data[0].currentSingleCost
                }
              })
            },
            render(h) {
              console.log('成本', this.record)
              return (
                <div>
                  {this.stockCost}
                </div>
              )
            }
          }
        },
        {
          property: 'unitCost',
          label: '成本单价',
          component: {
            props: ['record'],
            render(h) {
              return (
                <div>
                  {(this.record.cost / this.record.quantity).toFixed(2)}
                </div>
              )
            }
          }
        },
        {
          property: 'cost',
          label: '合计成本'
        },
        {
          property: 'profit',
          label: '利润'
        }
      ]
    }
  }
}
